$(document).ready(function() {
	fillWindowsInScreen($('#leftFullPanel'));
	fillWindowsInScreen($('#rightFullPanel'));
	setEqualHeight($("#rightFullPanel .customBox .block"));
	initDatepicker("#datepicker");
	initDatepicker("#birthFrom");
	initDatepicker("#birthTo");
	initDatepicker("#registrationFrom");
	initDatepicker("#registrationTo");
	initAgeSlider();
	$("input:submit, input:reset").button();
});

function getMaxHeight() {
	var headerHeight = 100;
	var footerHeight = 35;
	var paddings = 26;
	var headerBoxHeight = 26;
	var indentationPanel = 10;
	return $(window).height() - headerHeight - footerHeight - paddings - headerBoxHeight - indentationPanel;
}

function fillWindowsInScreen(obj) {
	var height = getMaxHeight();
	obj.height(height);
}

function initDatepicker(datepicker) {
	$.datepicker.setDefaults($.extend({
		minDate: new Date(1900,0,1),
		maxDate: new Date(),
		yearRange: '1900:+00',
		changeMonth: true,
		changeYear: true},
		$.datepicker.regional['']));
	
	$(datepicker).datepicker();
	var language = (window.navigator.userLanguage || window.navigator.language).substring(0,2);
	$(datepicker).datepicker('option', $.datepicker.regional[language.lenght == 0 ? 'en' : language]);
	$(datepicker).datepicker('option', 'dateFormat', 'dd/mm/yy');
	
	rangeDate('#birthFrom, #birthTo');
	rangeDate('#registrationFrom, #registrationTo');
}

function setDefaults(group) {
	group.each(function() {
		$(this).datepicker('option', 'maxDate', new Date());
		$(this).datepicker('option', 'minDate', new Date(1900,0,1));
	});
}

function rangeDate(group) {
	var dates = $(group).datepicker({
		onSelect: function(selectedDate) {
			var option = this.id == "birthFrom" || this.id == "registrationFrom" ? "minDate" : "maxDate",
			instance = $(this).data("datepicker"),
			date = $.datepicker.parseDate(instance.settings.dateFormat, selectedDate, instance.settings);
			dates.not(this).datepicker("option", option, date);
		}
	});	
}

function setEqualHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function validateRequiredFields(form) {
	var allFieldsComplete = true;
	requiredFields = form.find('.required');
	requiredFields.each(function() {
	    if(this.value.trim().length == 0) {
	    	allFieldsComplete = false;
	    }
	});
	if(!allFieldsComplete){
	    $('#dialog').dialog({
			height: 160,
			modal: true,
			resizable: false,
			buttons: {
					  OK: function() {
						  $(this).dialog("close");
					  }
			}
		});
	}
	return allFieldsComplete;
}

function initAgeSlider() {
	$("#ageSlider").slider({
		range: true,
		min: 1,
		max: 100,
		values: [20, 40],
		slide: function(event, ui) {
			$('#ageSliderResult')
								 .val(ui.values[0] + " - " + ui.values[1])
								 .show();
			$('#ageRangeRadio').attr("checked", true);
		}
	});
	$('#ageSliderResult').val($('#ageSlider').slider("values", 0) +
			" - " + $('#ageSlider').slider("values", 1));
	
	$('#ageSliderResult').hide();
	
	$('#ageRangeRadio').click(function() {
		$('#ageSliderResult').show();
	});
}
